// pages/components/mytree/mytree.js
Component({
	properties: {
		//树形结构数据
		model: Array,
		//树形结构展开层次  方式
		treeType: Number,
		//配合树形结构数据展开处理
		treeRank: {
			type: Number,
			value: 1
		},
		//选择模式 radio|单选  checkbox|多选  radio-checkbox|单选多选混
		chooseType: {
			type: String,
			value: 'radio'
		},
		// //分组划分-配合radio使用 1|最顶层为一组  2|每个父级为一组
		// groupType: {
		// 	type: Number,
		// 	value: 1
		// },
		//开启选中父级_checkbox子集全选 radio子集第一个值选中 true|开启  false|关闭
		selectAll: {
			type: Boolean,
			value: true
		},
		//选择自己，逐级父级关联
		selectRelevance:{
			type: Boolean,
			value: true
		},
		//递归存储层次下标
		indexString: {
			type: String,
			value: ''
		}
		// treeRank: Number
	},
	data: {
		indexShow: [], //展开项
		//indexArr:''
		indexArr: [], // 下标数组
	},

	methods: {
		toggle: function(item) {
			let _this = this
			let cacheArr = []
			_this.data.indexShow.forEach((e, i) => {
				if (item.currentTarget.dataset.index == i) {
					cacheArr.push(!e)
				} else {
					cacheArr.push(e)
				}
			})
			_this.setData({
				indexShow: cacheArr
			})
		},
		tapItem: function(e) {
			var index = e.currentTarget.dataset.indexstring;
			var _this = this
			let _Data = _this.data.model[e.currentTarget.dataset.index]
			let isChecked= !(_Data.checked?true:false);
			if(_Data.disable){
				return false;
			}
			if(_this.data.chooseType == 'radio'){
				_Data.checked = true
			}else if(_this.data.chooseType == 'checkbox'){
				_Data.checked = isChecked
			}
			if(_Data.childMenus){
				if(_Data.childMenus.length>0){
					_Data.childMenus = _this.forEachChil(_Data.childMenus,_this.data.chooseType,isChecked)
				}
			}
			this.triggerEvent('tapitem', {
				'index': index,
				'chooseType': _this.data.chooseType,
				'selectAll': _this.data.selectAll,
				'selectRelevance': _this.data.selectRelevance,
				'data':_Data
			}, {
				bubbles: true,
				composed: true
			});
		},
		forEachChil(data,type,isChecked){
			  let _this = this
			  if(type == 'radio'){
				  for (let i = 0; i < data.length; i++) {
					  if(!data[i].disable){
						  data[i].checked = isChecked
						  if(data[i].childMenus){
								  if(data[i].childMenus.length>0){
									  _this.forEachChil(data[i].childMenus,'radio',isChecked)
								  }
						  }
						  break;
					  }
				  }
				  
			  }else if(type == 'checkbox'){
				  for (let i = 0; i < data.length; i++) {
				  			  if(!data[i].disable){
				  				  data[i].checked = isChecked
				  			  }
				  			if(data[i].childMenus){
				  				if(data[i].childMenus.length>0){
				  					_this.forEachChil(data[i].childMenus,'checkbox',isChecked)
				  				}
				  			}
				  			
				  }
			  }
			  return data
		},
	},
	
	ready: function(e) {
		let cacheArr = [];
		let cacheIndex = [];
		let _this = this
		this.data.model.forEach((e, i) => {
			switch (_this.data.treeType) {
				case 0: //所有关闭
					cacheArr.push(false)
					break;
				case 1: //第一个第一级展开
					cacheArr.push((_this.data.treeRank == 1 && i == 0) ? true : false);
					break;
				case 2: //第一组所有展开
					cacheArr.push(_this.data.treeRank == 1 ? true : false);
					break;
				case 3: //递归所有第一级展开 
					cacheArr.push(i == 0 ? true : false)
					break;
				case 4: //展开所有菜单 
					cacheArr.push(true)
					break;
				default:
					cacheArr.push(false)
			}
			cacheIndex.push(_this.data.indexString ? (_this.data.indexString + '-' + i) : i)
		})
		this.setData({
			indexShow: cacheArr,
			indexArr: cacheIndex
		});
	},
	
})
